import {
	user_login_api,
	http_logout,
	get_user_info,
	get_agree_api 
} from '@/common/api.js';

export default {
	data() {
		return {

		}
	},
	methods: {
		
		// 返回距离  单元米
		distance(lat1, lng1, lat2, lng2) {
			lat1 = parseFloat(lat1)
			lng1 = parseFloat(lng1)
			lat2 = parseFloat(lat2)
			lng2 = parseFloat(lng2)

			var radLat1 = lat1 * Math.PI / 180.0;
			var radLat2 = lat2 * Math.PI / 180.0;
			var a = radLat1 - radLat2;
			var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
			var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
				Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
			s = s * 6378.137; // EARTH_RADIUS;
			s = Math.round(s * 10000) / 10000;

			s = s * 1000
			
			if (isNaN(s)) {
				return null;
			}
			
			return s.toFixed(2);
		},
		doAfterLogin(f) {
			if (!this.vuex_has_login) {
				uni.showLoading({
					title: '加载中'
				});
				let data = {}
				new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success: (res) => {
							data.code = res.code
							resolve()
						},
						fail: (err) => {
							reject(err)
						},
					})
				}).then(() => {
					// console.log('http request');
					// console.log(data);
					user_login_api(data)
						.then((res) => {
							console.log(res);
							this.$u.toast(res.msg, 2000)
							if (1 == res.code) {
								this.vuex_login(res)
							}
							uni.hideLoading()
						})
						.then(() => {
							f()
						})
				}).catch((e) => {
					uni.hideLoading()
					console.log(e);
				})
			} else {
				f()
			}
		},
		updateUserInfo() {
			get_user_info().then(res => {
				this.$u.vuex('vuex_user', {
					...this.vuex_user,
					...res.data
				})
			})
		},
		makePhoneCall(mobile) {
			uni.makePhoneCall({
				phoneNumber: mobile //仅为示例
			});
		},

		openLocation(lat, lng, address) {
			uni.openLocation({
				latitude: parseFloat(lat),
				longitude: parseFloat(lng),
				address: address,
				success: function(res) {
					console.log(res)
				},
				fail: function(err) {
					console.log(err)
				}
			});
		},
		wx_login() {
			this.doAfterLogin(() => {})
		},

		logout() {
			http_logout()
				.then(res => {
					if (1 == res.code) {
						this.vuex_logout()
					} else {
						this.$u.toast('退出失败，请联系管理员', 2000)
					}
				})
		},

		/*工具函数*/
		strSub(str, len) {
			if (str) {
				if (str.length > len) {
					return str.substr(0, len) + '\n' + this.strSub(str.substr(len), len)
				} else {
					let reason = str
					return reason
				}
			}
			return str
		},


	}
}